﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using Microsoft.Win32;
using System.Windows;
using System.Xml;

namespace SHomeWorkshop.LunarMind.Commands
{
    /// <summary>
    /// 以此类为模板，制作自定义命令类
    ///     共需要修改１０处！！！
    /// ==============================
    /// 
    /// 向左选取命令。
    /// ★①，修改类名。
    /// </summary>
    public static class MindViewSelectLeftCommand
    {
        private static RoutedUICommand routedUICmd;//创建私有字段

        public static RoutedUICommand RoutedUICmd//创建只读静态属性
        {
            get { return routedUICmd; }
        }

        /// <summary>
        /// ★②，修改静态构造方法名。
        /// </summary>
        static MindViewSelectLeftCommand()//类型构造器
        {
            //★③，修改两个字符串参数名。★④以及第三个参数的类型名。
            routedUICmd = new RoutedUICommand(
                "MindViewSelectLeftCommand",
                "MindViewSelectLeftCommand",
                typeof(MindViewSelectLeftCommand),//创建RoutedUICommand对象
                new InputGestureCollection() 
                { 
                    //★⑤，修改此处两个参数，以便挂接快捷键。
                    new KeyGesture(Key.Left,ModifierKeys.None,"Left")
                });

            cmdBinding.Command = routedUICmd;
            cmdBinding.CanExecute += new CanExecuteRoutedEventHandler(cmdBinding_CanExecute);
            cmdBinding.Executed += new ExecutedRoutedEventHandler(cmdBinding_Executed);
        }

        private static CommandBinding cmdBinding = new CommandBinding();

        public static CommandBinding CmdBinding
        {
            get { return cmdBinding; }
        }

        static void cmdBinding_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            //★⑦，修改此方法的实现。

            Execute();
        }

        internal static void Execute()
        {
            if (Globals.MainWindow.SelectedTitleImage != null)
            {
                TitleImage curImage = Globals.MainWindow.SelectedTitleImage;
                Title masterTitle = Globals.MainWindow.SelectedTitleImage.MasterTitle;
                if (masterTitle == null) return;

                if (masterTitle.ImgStackPanel.Children.Contains(curImage))
                {
                    int index = masterTitle.ImgStackPanel.Children.IndexOf(curImage);
                    if (index > 0)
                    {
                        TitleImage newSelectImg = masterTitle.ImgStackPanel.Children[index - 1] as TitleImage;
                        newSelectImg.IsSelected = true;
                        masterTitle.Editor.SelectedTitleImage = newSelectImg;
                        return;
                    }
                    else
                    {
                        masterTitle.SelectOnlySelf();
                    }
                }
                return;
            }

            if (Globals.SelectedTitlesList.Count <= 0)
            {
                var selComment = Globals.MainWindow.SelectedComment;
                if (selComment != null && selComment.MasterTitle != null)
                {
                    selComment.MasterTitle.SelectOnlySelf(); return;
                }

                var selPictureBox = Globals.MainWindow.SelectedPictureBox;
                if (selPictureBox != null && selPictureBox.MasterTitle != null)
                {
                    selPictureBox.MasterTitle.SelectOnlySelf(); return;
                }

                Globals.MainWindow.RootTitle.SelectOnlySelf();
            }
            else if (Globals.SelectedTitlesList.Count > 1)
            {
                Title mainSelectedTitle = null;
                foreach (Title t in Globals.SelectedTitlesList)
                {
                    if (t.IsMainSelectedTitle)
                    {
                        mainSelectedTitle = t;
                    }
                }

                if (mainSelectedTitle == null)
                {
                    Globals.MainWindow.RootTitle.SelectOnlySelf();
                }
                else
                {
                    if (mainSelectedTitle.IsFixed)
                    {
                        Title higher = mainSelectedTitle.GetHigherLvelTitle();
                        if (higher != null)
                        {
                            higher.SelectOnlySelf();
                        }
                        else
                        {
                            Globals.MainWindow.RootTitle.SelectOnlySelf();
                        }
                    }
                    else
                    {
                        if (mainSelectedTitle.IsFixed)
                        {
                            Title higher = mainSelectedTitle.GetHigherLvelTitle();
                            if (higher != null)
                            {
                                higher.SelectOnlySelf();
                            }
                            else
                            {
                                Globals.MainWindow.RootTitle.SelectOnlySelf();
                            }
                        }
                        else
                        {
                            mainSelectedTitle.SelectOnlySelf();
                        }
                    }
                }
            }
            else
            {
                //分情况：
                Title t = Globals.SelectedTitlesList[0];
                if (t == Globals.MainWindow.RootTitle)
                {
                    //如果当前标题是根标题，选中左侧[一级标题]中Index最小的一个。
                    Title leftDestTitle = null;
                    int minIndex = t.LowerLevelTitles.Count - 1;

                    foreach (Title tmp in t.LowerLevelTitles)
                    {
                        if (tmp.Direction == TitleDirection.Left && tmp.Index < minIndex)
                        {
                            minIndex = tmp.Index;
                        }
                    }

                    if (minIndex >= 0)
                    {
                        leftDestTitle = t.LowerLevelTitles[minIndex];
                        leftDestTitle.SelectOnlySelf();
                    }

                    return;
                }

                if (t.IsFixed)
                {
                    Title higher = t.GetHigherLvelTitle();
                    if (higher != null)
                    {
                        higher.SelectOnlySelf();
                    }
                    return;
                }

                //有概要就选定概要
                var mainSelTitle = Globals.SelectedTitlesList[0];
                if (mainSelTitle != null && mainSelTitle.GetLowerLevelTitles().Count <= 0 && mainSelTitle.SummaryComment != null)
                {
                    mainSelTitle.SummaryComment.SelectOnlySelf(); return;
                }

                //普通标题，直接改SelectNext();
                if (t.Direction == TitleDirection.Left)
                {
                    Globals.MainWindow.SelectNextOrShowFillBlank();
                }
                else
                {
                    //Globals.MainWindow.SelectPreview();
                    var curSelTitle = Globals.MainWindow.MainSelectedTitle;
                    if (curSelTitle != null)
                    {
                        var curHigherTitle = curSelTitle.GetHigherLvelTitle();
                        if (curHigherTitle != null)
                        {
                            curHigherTitle.SelectOnlySelf();
                        }
                    }
                }

                //if (t.Direction == TitleDirection.Left)
                //{
                //    if (t.CollapsedLowers)
                //    {
                //        //MessageBox.Show("左侧下级公式被折叠，不能选取。请先展开此标题。",
                //        //    Globals.AppName, MessageBoxButton.OK, MessageBoxImage.Warning);
                //        //return;

                //        ModifingItem mi = new ModifingItem()
                //        {
                //            IsNeedRefreshTitleAndLinesLocation = true,
                //            ModifingName = "展开标题",
                //            OldSelectedTitleID = t.ID,
                //        };

                //        Action act = new Action(t.ID, -1, t.GetType().Name,
                //            "CollapsedLowers", true.ToString(), false.ToString());

                //        t.CollapsedLowers = false;

                //        mi.Add(act);

                //        Globals.MainWindow.Do(mi);
                //        Globals.MainWindow.IsModified = true;
                //        Globals.MainWindow.RefreshTitleAndLinesLocation();
                //    }

                //    List<Title> lowers = t.LowerLevelTitles;
                //    if (lowers != null && lowers.Count > 0)
                //    {
                //        lowers[0].SelectOnlySelf(); return;
                //    }
                //}
                //else
                //{
                //    Title higher = t.GetHigherLvelTitle();
                //    if (higher != null)
                //    {
                //        higher.SelectOnlySelf(); return;
                //    }
                //}
            }
        }

        static void cmdBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            //★⑧，修改此方法的实现。
            e.CanExecute = true;//总是可用。
        }
    }
}
